"""
文件 Request VO
"""

from fastapi import UploadFile

import utils.enum_utils as enum
from models import (
    ListRequestBase,
    QueryFieldBase,
    RequestBase,
)
from my_typing import *
from utils.data_utils.special import ObjectIdStr
from utils.validator_utils.pydantic_validator.field import (
    BodyCharField,
    BodyListField,
    FormCharField,
    FormFileField,
    FormNormalField,
    PathCharField,
    QueryCharField,
)


class FilePathField:
    """ 文件 | 路径 | 字段 """

    file_id: ObjectIdStr = PathCharField(..., title="文件 ID")


class FileBatchDownloadRequest(RequestBase):
    """ 文件 | 批量下载 | 请求 """

    file_id_list: list[ObjectIdStr] = BodyListField(..., title="文件 ID 列表", min_length=1, max_length=10, unique=True)


class FileDetailRequest(RequestBase):
    """ 文件 | 详情 | 请求 """

    file_id: ObjectIdStr = FilePathField.file_id


class FileListRequest(ListRequestBase):
    """ 文件 | 列表 | 请求 """

    # -------------------- 排序字段 --------------------
    sort_by_create_time: enum.MongoSortOrNone = QueryCharField(None, title="根据创建时间排序")
    sort_by_filename: enum.MongoSortOrNone = QueryCharField(None, title="根据自定义文件名排序")
    sort_by_size: enum.MongoSortOrNone = QueryCharField(None, title="根据文件大小排序")

    # -------------------- 查询字段 --------------------
    filename: StrOrNone = QueryCharField(None, title="根据文件名模糊查询")
    is_public: BoolOrNone = QueryCharField(None, title="是否查看公开的文件")

    @classmethod
    def from_query(
        cls,
        page: int = QueryFieldBase.page,
        page_size: int = QueryFieldBase.page_size,
        sort_by_create_time: enum.MongoSortOrNone = QueryCharField(None, title="根据创建时间排序"),
        sort_by_filename: enum.MongoSortOrNone = QueryCharField(None, title="根据自定义文件名排序"),
        sort_by_size: enum.MongoSortOrNone = QueryCharField(None, title="根据文件大小排序"),
        filename: StrOrNone = QueryCharField(None, title="根据文件名模糊查询"),
        is_public: BoolOrNone = QueryCharField(None, title="是否查看公开的文件"),
    ) -> "FileListRequest":
        return cls(
            page=page,
            page_size=page_size,
            sort_by_create_time=sort_by_create_time,
            sort_by_filename=sort_by_filename,
            sort_by_size=sort_by_size,
            filename=filename,
            is_public=is_public,
        )


class FileUpdateRequest(RequestBase):
    """ 文件 | 更新 | 请求 """

    file_id: ObjectIdStr | None = BodyCharField(None, title="文件 ID")
    filename: str = BodyCharField(..., title="自定义文件名", max_length=64)
    is_public: bool = BodyCharField(..., title="是否查看公开的文件")


class FileUploadRequest(RequestBase):
    """ 文件 | 上传 | 请求 """

    file: UploadFile = FormFileField(..., title="文件")
    filename: str = FormCharField("", title="自定义文件名", max_length=64)
    is_public: bool = FormNormalField(True, title="是否为公开的文件")

    @classmethod
    def as_form(
        cls,
        file: UploadFile = FormFileField(..., title="文件"),
        filename: str = FormCharField("", title="自定义文件名", max_length=64),
        is_public: bool = FormNormalField(True, title="是否为公开的文件"),
    ):
        return cls(
            file=file,
            filename=filename,
            is_public=is_public,
        )
